//
//  AISTabViewController.m
//  ParkingCloud
//
//  Created by Ares on 16/3/16.
//  Copyright © 2016年 NJSubier. All rights reserved.
//

#import "AISTabViewController.h"


@interface AISTabViewController () <UIScrollViewDelegate> {
    NSInteger selectedTab;
    CGFloat statusBarHeight;
    CGFloat navigationBarHeight;
    CGFloat layoutStartPosY;

}

@property (nonatomic, strong) UIScrollView *contentScrollView;
@property (nonatomic, strong) NSMutableArray *tabButtons;
@property (nonatomic, strong) UIView *indicatorView;
@property (nonatomic, strong) UIView *bottomHeaderView;

@end

@implementation AISTabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    // Do any additional setup after loading the view.
    statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
    navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
    layoutStartPosY = statusBarHeight + navigationBarHeight;
    
    if (0 == self.tabHeight) {
        self.tabHeight = 40.0;
    }
    if (nil == self.tabTitleNormalColor) {
        self.tabTitleNormalColor = [UIColor blackColor];
    }
    if (nil == self.tabSeparatorColor) {
        self.tabSeparatorColor = [UIColor colorWithWhite:0.7 alpha:1.0];
    }
    if (nil == self.tabIndicatorColor) {
        self.tabIndicatorColor = [UIColor blueColor];
    }
    if (0 == self.tabIndicatorHeight) {
        self.tabIndicatorHeight = 5;
    }
    if (nil == self.tabBottomLineColor) {
        self.tabBottomLineColor = [UIColor blueColor];
    }
    if (0 == self.tabBottomLineHeight) {
        self.tabBottomLineHeight = 1;
    }
    
    if (nil != self.viewControllers) {
        [self loadViewControllers];
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
    navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
    layoutStartPosY = statusBarHeight + navigationBarHeight;
    
    [self.contentScrollView setFrame:CGRectMake(0.0, layoutStartPosY + self.tabHeight, self.view.frame.size.width, self.view.frame.size.height - layoutStartPosY - self.tabHeight)];
    
    for (int i = 0; i < [self.viewControllers count]; i ++) {
        UIViewController *controller = [self.viewControllers objectAtIndex:i];
        [[controller view] setFrame:CGRectMake(i * self.contentScrollView.frame.size.width, 0.0, self.contentScrollView.frame.size.width, self.contentScrollView.frame.size.height)];
    }
    
    float tabWidth = self.contentScrollView.frame.size.width / [self.viewControllers count];
    for (int i = 0; i < [self.tabButtons count]; i ++) {
        UIButton *tab = [self.tabButtons objectAtIndex:i];
        [tab setFrame:CGRectMake(i * tabWidth, layoutStartPosY, tabWidth, self.tabHeight)];
    }
    [self.bottomHeaderView setFrame:CGRectMake(0.0, layoutStartPosY + self.tabHeight - self.tabBottomLineHeight, self.contentScrollView.frame.size.width, self.tabBottomLineHeight)];
    [self.indicatorView setFrame:CGRectMake(0.0, layoutStartPosY + self.tabHeight - self.tabIndicatorHeight, tabWidth, self.tabIndicatorHeight)];
    
    [self.contentScrollView setContentSize:CGSizeMake(self.contentScrollView.frame.size.width * [self.viewControllers count], self.contentScrollView.frame.size.height)];
    
    [self selectTabAtIndex:self.selectedTab];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Functions

- (void)loadViewControllers {
    self.contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, layoutStartPosY + self.tabHeight, self.view.frame.size.width, self.view.frame.size.height - layoutStartPosY - self.tabHeight)];
    [self.contentScrollView setPagingEnabled:YES];
    [self.contentScrollView setDelegate:self];
    self.contentScrollView.bounces = NO;
    [self.contentScrollView setShowsHorizontalScrollIndicator:NO];
    
    float tabWidth = self.view.frame.size.width / [self.viewControllers count];
    self.tabButtons = [[NSMutableArray alloc] init];
    for (int i = 0; i < [self.viewControllers count]; i++) {
        // Create content view
        UIViewController *controller = [self.viewControllers objectAtIndex:i];
        [self addChildViewController:controller];
        
        [[controller view] setFrame:CGRectMake(i * self.contentScrollView.frame.size.width, 0.0, self.contentScrollView.frame.size.width, self.contentScrollView.frame.size.height)];
        [self.contentScrollView addSubview:[controller view]];
        
        // Create button
        UIButton *tab = [[UIButton alloc] initWithFrame:CGRectMake(i * tabWidth, layoutStartPosY, tabWidth, self.tabHeight)];
        [tab setTitle:controller.title forState:UIControlStateNormal];
        [tab setTitleColor:self.tabTitleNormalColor forState:UIControlStateNormal];
        [tab setTitleColor:self.tabTitleSelectedColor forState:UIControlStateSelected];
        [tab addTarget:self action:@selector(selectTab:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:tab];
        [self.tabButtons addObject:tab];
        
        // Add separator
        if(i > 0) {
            UIView *sep = [[UIView alloc] initWithFrame:CGRectMake(i * tabWidth, layoutStartPosY + 10, 1, 20)];
            [sep setBackgroundColor:self.tabSeparatorColor];
            [self.view addSubview:sep];
        }
    }
    [self.contentScrollView setContentSize:CGSizeMake(self.contentScrollView.frame.size.width * [self.viewControllers count], self.contentScrollView.frame.size.height)];
    
    UIButton *tab = [self.tabButtons objectAtIndex:0];
    [tab setSelected:YES];
    selectedTab = 0;
    
    if (nil == self.bottomHeaderView) {
        self.bottomHeaderView = [[UIView alloc] init];
    }
    self.bottomHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0.0, layoutStartPosY + self.tabHeight - self.tabBottomLineHeight, self.view.frame.size.width, self.tabBottomLineHeight)];
    [self.bottomHeaderView setBackgroundColor:self.tabBottomLineColor];
    [self.view addSubview:self.bottomHeaderView];
    
    if (nil == self.indicatorView) {
        self.indicatorView = [[UIView alloc] init];
    }
    self.indicatorView.frame = CGRectMake(0.0, layoutStartPosY + self.tabHeight - self.tabIndicatorHeight, tabWidth, self.tabIndicatorHeight);
    [self.indicatorView setBackgroundColor:self.tabIndicatorColor];
    [self.view addSubview:self.indicatorView];
    
    [self.view addSubview:self.contentScrollView];
}

- (void)selectTab:(id)sender {
    selectedTab = [self.tabButtons indexOfObject:sender];
    CGRect rect = CGRectMake(self.view.frame.size.width * selectedTab, 0.0, self.view.frame.size.width, self.contentScrollView.contentSize.height);
    [self.contentScrollView scrollRectToVisible:rect animated:YES];
    [self deselectAllTabs];
    [sender setSelected:YES];
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(tabViewDidSelectedAtIndex:)]) {
        [self.delegate tabViewDidSelectedAtIndex:selectedTab];
    }
}

- (void)deselectAllTabs {
    for (UIButton *tab in self.tabButtons) {
        [tab setSelected:NO];
        [tab setTitleColor:self.tabTitleNormalColor forState:UIControlStateNormal];
    }
}

- (void)selectTabAtIndex:(NSInteger)index {
    if(index < 0 || index >= [self.tabButtons count]) {
        return;
    }
    UIButton *tab = [self.tabButtons objectAtIndex:index];
    [self selectTab:tab];
}

- (NSInteger)selectedTab {
    return selectedTab;
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat width = scrollView.frame.size.width;
    int page = (scrollView.contentOffset.x + (0.5f * width)) / width;
    float tabWidth = self.indicatorView.frame.size.width;
    
    CGRect indicatorFrame = self.indicatorView.frame;
    indicatorFrame.origin.x = page *tabWidth;
    [self.indicatorView setFrame:indicatorFrame];
    
    [self deselectAllTabs];
    UIButton *tab = [self.tabButtons objectAtIndex:page];
    [tab setSelected:YES];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
